<template>
  <div class="container">
    <div class="top">
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="monthrange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="orgAreaName"
        label="区域名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="orgName"
        label="机构名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="productClassName"
        label="商品品类名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="money"
        label="金额"
        align="center"
      ></el-table-column>
      <el-table-column prop="num" label="数量" align="center"></el-table-column>
      <el-table-column prop="dt" label="时间" align="center"></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import { getProductClassOrgReportDataWeb } from "@/api/OrgReportData/OrgReportDataList";
import { getToken } from "@/utils/auth";

export default {
  components: {
    pagination: Pagination,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      date: [],
      search: {
        start_date: "",
        end_date: "",
      },
      //默认时间
      normal_startime:
        new Date().getFullYear() + "-" + new Date().getMonth() + "-" + "01",
      normal_endtime:
        new Date().getFullYear() +
        "-" +
        (new Date().getMonth() + 1) +
        "-" +
        "01",
    };
  },
  created() {
    this.search.start_date = this.normal_startime;
    this.search.end_date = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_date);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_date);
    }
    //页面数据
    // this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data =
        "startDate=" +
        this.search.start_date +
        "&endDate=" +
        this.search.end_date +
        "&pageSize=" +
        this.pageData.pageSize +
        "&currPage=" +
        this.pageData.page;
      getProductClassOrgReportDataWeb(data)
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data.content;
            this.pageData.page = res.data.currentPage;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 本地搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_date = this.date[0];
        this.search.end_date = this.date[1];
      }
      this.getData();
    },
    //清空
    empty() {
      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        start_date: this.normal_startime,
        end_date: this.normal_endtime,
      };
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURLs +
            "/reportController/getProductClassOrgReportData?token=" +
            getToken() +
            "&startDate=" +
            this.search.start_date +
            "&endDate=" +
            this.search.end_date;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>